package com.jeehentor.config.security.manager;

import com.jeehentor.common.api.SystemCommonAPI;
import com.jeehentor.common.api.vo.LoginUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Description :数据库方式用户信息接口
 * @Author : wzd
 * @Date : 2024/4/8 17:10
 * @Version : 1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class DbUserDetailsManager implements UserDetailsService {

    private final SystemCommonAPI systemCommonApi;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LoginUser userByName = systemCommonApi.findUserByName(username);
        if(userByName==null){
            log.info("用户不存在");
            throw new UsernameNotFoundException("用户不存在");
        }
        //TODO 部门数据权限
        //权限
        userByName.setPermissions(systemCommonApi.getPermissionList(userByName.getId()));
        //角色
        userByName.setRoles(systemCommonApi.getRoleList(userByName.getId()));
        return userByName;
    }


}
